package com.uniknow.app.adapter;

import java.util.List;

import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.View.OnLongClickListener;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.TextView;

import com.uniknow.app.AppConfig;
import com.uniknow.app.R;
import com.uniknow.app.bin.NewsBean;
import com.uniknow.app.util.BitmapManager;
import com.uniknow.app.util.ImageUtils;
import com.uniknow.app.util.StringUtils;

public class NewsAdapter extends BaseAdapter {
	private Context mContext;
	private int pageNumber = 0,pageSize = 10;
	private BitmapManager bmpManager;//缓存储图片资源
	private Bitmap defaultBmp;//加载失败后默认显示图片
	
	private OnClickListener itemOnClick;
	private OnLongClickListener itemOnLongClick;
	
	private LayoutInflater 				listContainer;//视图容器
	private int 						itemViewResource;//自定义项视图源
	private List<NewsBean> list;
	
	static class ListItemView{				//自定义控件集合  
		public TextView author; 
		public TextView date; 
		public ImageView img; 
        public TextView title;
        public TextView text;
	     
	}
	
	public NewsAdapter(Context context,List<NewsBean> listData){
		mContext = context;
		this.listContainer = LayoutInflater.from(mContext);	//创建视图容器并设置上下文
		this.itemViewResource = R.layout.item_news_show;
		list = listData;
		bmpManager = new BitmapManager(BitmapFactory.decodeResource(mContext.getResources(), R.drawable.empty_image));
		defaultBmp = ImageUtils.drawableToBitmap(context.getResources().getDrawable(R.drawable.empty_image));//BitmapFactory.decodeFile(R.drawable.about_button_devider_level);

	}

	@Override
	public int getCount() {
		return list==null?0:list.size();
	}

	@Override
	public NewsBean getItem(int arg0) {
		if(list==null){
			return null;
		}
		return list.get(arg0);
	}

	@Override
	public long getItemId(int arg0) {
		return list.get(arg0).getId();
	}

	@Override
	public View getView(int position, View convertView, ViewGroup parent) {
		//自定义视图
		ListItemView  listItemView = null;
		NewsBean newsBean = list.get(position);
		if (convertView == null) {
			convertView = listContainer.inflate(this.itemViewResource, null);
			
			listItemView = new ListItemView();
			//获取控件对象
			listItemView.author = (TextView)convertView.findViewById(R.id.item_news_show_author);
			listItemView.date = (TextView)convertView.findViewById(R.id.item_news_show_date);
			listItemView.img = (ImageView)convertView.findViewById(R.id.item_news_show_img);
			listItemView.title = (TextView)convertView.findViewById(R.id.item_news_show_title);
			listItemView.text = (TextView)convertView.findViewById(R.id.item_news_show_text);
			
			//设置控件集到convertView
			convertView.setTag(R.id.tag_item_view,listItemView);//key-value
			
		}else{
			listItemView = (ListItemView)convertView.getTag(R.id.tag_item_view);
		}
		
		listItemView.author.setText(newsBean.getAuthor());
		listItemView.date.setText(StringUtils.friendly_time(newsBean.getCreateDate()));
		if(StringUtils.isEmpty(newsBean.getImg())){
			listItemView.img.setImageBitmap(null);
			listItemView.img.setVisibility(View.GONE);
		}else{
			bmpManager.loadBitmap(AppConfig.host_url+"/"+newsBean.getImg(), listItemView.img, defaultBmp);
			listItemView.img.setVisibility(View.VISIBLE);
		}
		listItemView.title.setText(newsBean.getTitle());
		listItemView.text.setText(newsBean.getText());
		
		convertView.setTag(R.id.tag_pos,position);
		convertView.setTag(R.id.tag_obj,list.get(position));
		convertView.setOnClickListener(itemOnClick);
		convertView.setOnLongClickListener(itemOnLongClick);
		
		return convertView;
	}
	
	public List<NewsBean> getList() {
		return list;
	}

	public void setList(List<NewsBean> list) {
		this.list = list;
	}

	public void setItemOnclickListener(OnClickListener itemOnClickListener){
		this.itemOnClick = itemOnClickListener;
	}
	
	public void setItemOnLongClickListener(OnLongClickListener itemOnLongClickListener){
		this.itemOnLongClick = itemOnLongClickListener;
	}

}
